// template.js you can generate
module.exports = {
  vueTemplate: path => {
    let p = path.split('/')
    let name = p[0] + p[1]
    return `<template>
    <basic-container>
      <avue-crud
        v-bind="bindVal"
        :permission="permission"
        v-on="onEvent"
        v-model="form"
        :page.sync="page" 
      ></avue-crud>
    </basic-container>
  </template>
<script>
export default window.$crudCommon(
  {
    name: '${name}',
    data() {
      return {};
    },
    methods: {} 
  },
  {
    name: "${path}", //模块名字
    list: "list", //列表接口名字
    update: "update", //更新接口名字
    add: "add", //新增接口名字
    del: "del", //删除接口名字
    rowKey: "_id", //后台返回的主键
    pageNumber: "pageNumber", //页码
    pageSize: "pageSize", //页数
    res: data => ({ data: data.data, total: data.total }), //列表的结构
    total: "total", //总页数
    data: "data", //列表属性
    role: {
      addBtn: "${path}-addBtn",
      editBtn: "${path}-editBtn",
      delBtn: "${path}-delBtn"
    }
  }
);
</script>
<style lang="scss" scoped>

</style>
`
  },
  apiTemplate: path => {
    return `import request from '@/router/axios';
    let url = '/http/admin/${path}'  //云函数后面的url
    // let url = '/http/test'  //test
    // 查询
    export const list = params => request({ url, method: 'get', params: { action: 'list', params } })
    // 添加
    export const add = params => request({ url, method: 'post', data: { action: 'add', params } })
    // 删除
    export const del = params => request({ url, method: 'post', data: { action: 'del', params } })
    // 修改
    export const update = params => request({ url, method: 'post', data: { action: 'update', params } })`
  },
  routerTemplate: (option) => {
    let templName = option.path
    return `export default {
      label: "${option.name}",
      path: '/${templName}',
      component: 'views/${templName}',
      icon: 'icon-caidan',
      position: 1,
      meta: {
        i18n: '${option.name}',
      },
      unique: '${templName}',
      children: [
        {
          nopath: true,
          unique: '${templName}-addBtn',
          label: '新增'
        },
        {
          nopath: true,
          unique: '${templName}-editBtn',
          label: '编辑'
        },
        {
          nopath: true,
          unique: '${templName}-delBtn',
          label: '删除'
        }
      ]
    }`
  },
  optionTemplate: (option, column) => {
    return `//${option.path}页面配置
    // eslint-disable-next-line no-unused-vars
    export default (self) => {
      //self => vue的this对象
      return {
        border: true,
        column: [${column}]
      }
    }`
  },
  MockTemplate: (componentName, column) => {
    return `import Mock from 'mockjs'
    let list = []
    for (let i = 0; i < 11; i++) {
      list.push(Mock.mock({"id": "@increment",${column}}))
    }
    export default ({ mock }) => {
      if (!mock) return;
      //查询
      Mock.mock('/Admin/${componentName}/list', 'get', (res) => {
        //可以自己写检索条件${column}
        const { pageNumber = 1, pageSize = 10 } =  JSON.parse(res.body)
        //let mockList = list.filter(item => {
          //可以自己写检索条件${column}
          //检索不到则返回false 
          // if (title && item.title.indexOf(title) < 0) return false
          //return true
        //})
        //const pageList = mockList.filter((item, index) => index < pageSize * pageNumber && index >= pageSize * (pageNumber - 1))
        const pageList = list.filter((item, index) => index < pageSize * pageNumber && index >= pageSize * (pageNumber - 1))
        return {
            "code": "200",
            "data": {
                "pageNumber": Number(pageNumber),
                "pageSize": Number(pageSize),
                "total": list.length,
                "list": pageList
            },
            "message": "成功",
            "success": true
          }
      })
    //增加
    Mock.mock('/Admin/${componentName}/add', 'put', (res) => {
      const data =  JSON.parse(res.body)
      list.push(data)
      return {
          "code": "200",
          "message": "成功",
          "success": true
        }
    })
    //修改
    Mock.mock('/Admin/${componentName}/update', 'post', (res) => {
      const data = JSON.parse(res.body)
      list.forEach((item, index) => {
          if (item.id == data.id) {
            list[index] = data
          }
      })
      return {
          "code": "200",
          "message": "成功",
          "success": true
        }
    })
    // 删除
    Mock.mock('/Admin/${componentName}/del', 'delete', (res) => {
      const data = JSON.parse(res.body)
      list.forEach((item, index) => {
          if (item.id == data.id) {
            list.splice(index, 1)
          }
      })
      return {
          "code": "200",
          "message": "成功",
          "success": true
        }
    })
    }`
  }
}
